package com.xjj.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xjj.entity.Manager;
import com.xjj.entity.Vote;
import com.xjj.entity.VoteOption;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface VoteMapper extends BaseMapper<Vote> {
    @Update("update xjj_vote set status='1' where id=#{voteId}")
    void cancelVote(Integer voteId);

    @Insert("insert into xjj_vote_option values(null,#{voteId},#{text},0)")
    void addVoteOption(@Param("voteId") Integer id, @Param("text") String s);

    @Select("select * from xjj_vote where id=#{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "sender_id",property = "senderId",id = true),
            @Result(property = "manager",column = "sender_id",one = @One(select = "com.xjj.mapper.VoteMapper.getManagerById")),
            @Result(property = "voteOption",column = "id",many = @Many(select = "com.xjj.mapper.VoteMapper.findOptionByVoteId"))
    })
    Vote getDetailsById(Integer id);

    @Select("select * from xjj_vote_option where vote_id=#{id}")
    List<VoteOption> findOptionByVoteId(Integer voteId);

    @Select("select count(*) from xjj_vote where id<=#{id}")
    Integer getRowNum(Integer id);

    @Select("select * from xjj_manager where id=#{id}")
    Manager getManagerById(Integer id);

    @Update("update xjj_vote set status='2' where id=#{voteId}")
    boolean voteTimeOut(Integer voteId);

    @Delete("delete from xjj_vote_option where vote_id = #{id}")
    void deleteOptionById(Integer voteId);

    @Insert("insert into xjj_vote_student values(#{optionId},#{studentId},#{voteId})")
    void addStudentVote(@Param("optionId") Integer optionId,
                       @Param("studentId") Integer studentId,
                       @Param("voteId") Integer voteId);

    @Update("update xjj_vote set voted_num=#{votedNum} where id=#{voteId}")
    void setVotedNum(@Param("voteId") Integer voteId,@Param("votedNum") Integer votedNum);

    @Select("select count(*) from xjj_vote_student where student_id=#{studentId} and vote_id=#{voteId}")
    Integer hasVoting(@Param("studentId") Integer studentId,@Param("voteId") Integer voteId);

    @Update("update xjj_vote_option set num=#{num} where id=#{id}")
    void setOptionNum(@Param("id") Integer id, @Param("num") Integer num);
}
